// miniprogram/pages/my/my_address/home/index.js
const app = getApp()


Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      this.selectType = options.type;
    } else {
      this.selectType = "update"
    }
  },
  init() {
    this.no = 1;
    var params = {};
    params.no = this.no;
    params.size = 10;
    app.dataRequest("/rest/customeraddress/my.htm", {}, this, "addressList");
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.info(this.totalPage);
    if (this.no < this.totalPage) {
      this.no = this.no + 1;
      this.setData({
        noMore: false
      });
      var params = {};
      params.no = this.no;
      params.size = 10;
      app.loadMore("/rest/customeraddress/my.htm", params, this, "addressList");

    } else {
      this.setData({
        noMore: true
      });
    }
  },
  deleteData(event) {
    var self = this;
    var id = event.target.dataset.id;
    if (id) {
      wx.showModal({
        title: '提示',
        content: '确认要删除该条数据吗',
        success(res) {
          if (res.confirm) {
            self.deleteAddress(id);
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })

    }
    console.info(event);
  },
  selectAddress(e) {
    console.info(e);
    var obj = e.currentTarget.dataset.obj;
    if (this.selectType == "select") {
      var ps = getCurrentPages();
      var len = ps.length - 2;
      console.info(len);
      if (len >= 0) {
        var page = ps[len];
          var data = {};
          data["selectAddress"] = obj;
          page.setData(data);
      }
      wx.navigateBack({
        delta: 0,
        success: (res) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/my_address/update/index?id=' + obj.id,
      })
    }

  },
  deleteAddress(id) {
    var params = {};
    params.id = id;
    app.postRequest("/rest/customeraddress/delete.htm", params, this.backDeleteData);

  },
  backDeleteData(res) {
    var self = this;
    if (res.code == 0) {
      app.showModal("删除成功", () => {
        self.init();
      });
    } else {
      app.showModal(res.msg);
    }
  }

})